package com.hnluchuan.bmjlearning.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import com.hnluchuan.bmjlearning.common.AgentStatus;
import com.hnluchuan.bmjlearning.common.DeleteStatus;
import com.hnluchuan.bmjlearning.model.User;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;
import org.apache.commons.lang3.StringUtils;
	
import com.hnluchuan.core.dao.BaseDAO;
import com.hnluchuan.bmjlearning.model.Agent;
import com.hnluchuan.bmjlearning.dto.AgentDTO;
import com.hnluchuan.core.support.Page;

@Repository
public class AgentDAO extends BaseDAO<Agent> {

    public List<Agent> find(AgentDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From Agent n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getUser() != null) {
        	if (dto.getUser().getId() != null) {
	            hql.append(" and n.user.id = ? ");
	            paras.add(dto.getUser().getId());
        	}
        }			
        if (dto.getStatus() != null) {
            hql.append(" and n.status = ? ");
            paras.add(dto.getStatus());
        }
        if (StringUtils.isNotBlank(dto.getMobile())) {
            hql.append(" and n.mobile = ? ");
            paras.add(dto.getMobile());
        }
        if (StringUtils.isNotBlank(dto.getOrgNameOrManager())) {
            hql.append(" and (n.orgName like ? or n.manager like ?) ");
            paras.add("%" + dto.getOrgNameOrManager() + "%");
            paras.add("%" + dto.getOrgNameOrManager() + "%");
        }
        if (StringUtils.isNotBlank(dto.getOrgName())) {
            hql.append(" and n.orgName like ? ");
            paras.add("%" + dto.getOrgName() + "%");
        }					
        if (StringUtils.isNotBlank(dto.getManager())) {
            hql.append(" and n.manager like ? ");
            paras.add("%" + dto.getManager() + "%");
        }
        hql.append(" order by n.id desc ");
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public Agent findOne(AgentDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<Agent> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public Agent findByUserId(Long userId){
        String hql = "From Agent a where a.user.id = ?";
        List<Object> paras = new ArrayList<>();
        paras.add(userId);

        return super.findOne(hql, paras.toArray());
    }

    public int deleteOrActivateAgentByAgentId(Long agentId, Integer status){
        String hql = "Update Agent a set a.status = ? where a.id = ?";
        List<Object> paras = new ArrayList<>();
        paras.add(status);
        paras.add(agentId);

        return super.executeUpdate(hql, paras.toArray());
    }

    public int deleteOrActivateUserByAgentId(Long userId, Integer status){
        String hql = "Update User u set u.deleted = ? where u.id = ?";
        List<Object> paras = new ArrayList<>();
        if(status == AgentStatus.Disabled.getValue()){
            paras.add(DeleteStatus.Deleted.getValue());
        }else {
            paras.add(DeleteStatus.NotDeleted.getValue());
        }
        paras.add(userId);

        return super.executeUpdate(hql, paras.toArray());
    }

    @Override
	public Long create(Agent agent) {
	    if (agent.getCreated() == null) {    
	        agent.setCreated(new Date());
	    }    
		return super.create(agent);
	}

	@Override
	public void update(Agent agent) {
		super.update(agent);
	}
    
}
